<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/9
 * Time: 15:54
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BowenController extends Controller
{
    public function getBowen(){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->where('bowen_id', '=', '1')->get();
        $data = [];
        $data['bowen'] = $result[0];
        $data['author'] = $this->getBowenAuthor($result[0]->bowen_user_id);
        $data['repost'] = null;
        if($result[0]->bowen_repost_id != null && $result[0]->bowen_repost_id != 'null'){
            $data['repost'] = $this->getRepost($result[0]->bowen_repost_id);
        }
        $this->response(200,"获取成功",$data);
    }

    public function getComments(Request $request){
        if($request->isMethod('post')){
            $bowen_id = $request->input('bowen_id');
            $comments = [];
            $comments['comments'] = $this->getBowenComments($bowen_id);
            $this->response(200,'获取成功',$comments);
        }else{
            $this->response(503,'非法请求',null);
        }
    }

    public function getReplys(Request $request){
        if($request->isMethod('post')){
            $comment_id = $request->input('comment_id');
            $replys = [];
            $replys['replys'] = $this->getCommentReplys($comment_id);
            $this->response(200,'获取成功',$replys);
        }else{
            $this->response(503,'非法请求',null);
        }
    }

    private function getRepost($bowen_id){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->where('bowen_id','=',$bowen_id)->get();
        $data = [];
        $data['bowen'] = $result[0];
        $data['author'] = $this->getBowenAuthor($result[0]->bowen_user_id);
        return $data;
    }

    private function getBowenAuthor($user_id){
        $users = DB::table('weibo_users');
        $result = $users->where('user_id', '=', $user_id)->select('user_id','user_nickname','user_level','user_head_img')->get();
        return $result[0];
    }

    private function getCommentAuthor($user_id){
        $users = DB::table('weibo_users');
        $result = $users->where('user_id', '=', $user_id)->select('user_id','user_nickname','user_level','user_head_img')->get();
        return $result[0];
    }

    private function getReplyAuthor($user_id){
        $users = DB::table('weibo_users');
        $result = $users->where('user_id', '=', $user_id)->select('user_id','user_nickname','user_level')->get();
        return $result[0];
    }

    private function getBowenComments($bowen_id){
        $comments = DB::table('weibo_bowen_comments');
        $result = $comments->where('comment_bowen_id','=',$bowen_id)->orderBy('comment_like_count', 'desc')->limit(7)->get();
        $data = [];
        for ($i = 0; $i < count($result); $i++){
            $data[$i] = [
                'comment'=>$result[$i],
                'author'=>$this->getCommentAuthor($result[$i]->comment_user_id),
                'replys'=>$this->getCommentReplys($result[$i]->comment_id)
            ];
        }
        return $data;
    }

    private function getCommentReplys($comment_id){
        $replys = DB::table('weibo_comment_replys');
        $result = $replys->where('reply_comment_id','=',$comment_id)->orderBy('reply_like_count','desc')->limit(7)->get();
        $data = [];
        for($i = 0; $i < count($result); $i++){
            $data[$i] = [
                'reply'=>$result[$i],
                'author'=>$this->getReplyAuthor($result[$i]->reply_user_id)
            ];
        }
        return $data;
    }

    private function response($status,$message,$data){
        $response = [];
        $response['status'] = $status;
        $response['message'] = $message;
        $response['data'] = $data;
        echo json_encode($response);
    }
}